
using Youz.Common.Entity;
using Youz.CreditRepay.BLL;
using Youz.Common.Helper;
using Youz.Common.BLL;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Youz.CreditRepay.Manage.Handlers
{
    public partial class Sys_VersionHandler : IHttpHandler, IRequiresSessionState
    {
        Sys_VersionBLL bll = new Sys_VersionBLL();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string _type = WebHelper.Get("type");
            string result = string.Empty;
            switch (_type)
            {
                case "get":
                    result = Get();
                    break;
                case "list":
                    result = List();
                    break;
                case "save":
                    result = Save();
                    break;
                case "delete":
                    result = Delete();
                    break;
            }

            context.Response.Write(result);
            context.Response.End();
        }

        private string Get()
        {
            Guid VersionID = WebHelper.Post<Guid>("VersionID");

            return bll.GetJson(VersionID);
        }

        private string List()
        {
            int pageIndex = WebHelper.Post<int>("start");
            int pageSize = WebHelper.Post<int>("length");
            string DeviceType = WebHelper.Post("DeviceType").Trim();
            string Status = WebHelper.Post("Status").Trim();
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" 1=1 ");
            if (!string.IsNullOrEmpty(DeviceType))
            {
                sb.AppendFormat(" and DeviceType = '{0}'", DeviceType);
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sb.AppendFormat(" and Status = '{0}'", Status);
            }
            int total = 0;
            DataTable dt = bll.Gets(sb.ToString(), pageIndex, pageSize, out total);

            return Utility.GetJsonByDataTableHaveTime(total, dt);
        }

        private string Save()
        {
            string id = WebHelper.Post("VersionID");
            bool isAdd = string.IsNullOrWhiteSpace(id);
            Sys_Version model = new Sys_Version();

            if (isAdd)
            {                
            }
            else
            {
                model = bll.Get(WebHelper.Post<Guid>("VersionID"));
            }

            var uploadMsg =  Core.UploadAppPackage(HttpContext.Current, "_Package");
            if(!uploadMsg.Result)
            {
                return Utility.AjaxResult(false, uploadMsg.Message);
            }

            model.VersionNo = WebHelper.Post<String>("VersionNo");
            model.DeviceType = (AppDeviceType)WebHelper.Post<Int32>("DeviceType");
            model.Package = uploadMsg.Object.ToString();
            model.PackageType = (AppPackageType)WebHelper.Post<Int32>("PackageType");
            model.Status = WebHelper.Post<Int32>("Status");
            model.IsForceUpdate = WebHelper.Post<Int32>("IsForceUpdate");
            model.Comment = WebHelper.Post<String>("Comment");
            model.Creater = Core.MemberAccount;
            model.CreateTime = DateTime.Now;

            if (isAdd ? bll.Add(model) : bll.Update(model))
            {
                return Utility.AjaxResult(true, AjaxResultType.Save);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Save);
            }
        }

        private string Delete()
        {
            if (bll.Delete(WebHelper.Post<Guid>("VersionID")))
            {
                return Utility.AjaxResult(true, AjaxResultType.Delete);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Delete);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

